package com.imooc.mall.exception;

import com.imooc.mall.common.ApiRestResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理的handler
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    //通过日志记录
    private final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    //表明这个方法处理的是关于exception异常的方法
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object handlerException(Exception e){
        log.error("Default Exception",e);
        return ApiRestResponse.error(ImoocMallExceptionEnum.SYSTEM_ERROR);
    }

    @ExceptionHandler(ImoocMallException.class)
    @ResponseBody
    public Object handlerMallException(ImoocMallException e){
        log.error("Default Exception",e);
        return ApiRestResponse.error(e.getCode(),e.getMessage());
    }
}
